/**
 * 自动提取windows10自带聚焦壁纸到用户的图片文件夹下
 * Author: 叶流痕
 */
let fs = require('fs');
let path = require('path');
let sizeOf = require('image-size');
let os = require('os');
let colors = require('colors');

colors.setTheme({ silly: 'rainbow', input: 'grey', verbose: 'cyan', prompt: 'red', info: 'green', data: 'blue', help: 'cyan', warn: 'yellow', debug: 'magenta', error: 'red'});

let sourcePath = path.join(os.homedir(), '\\AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\LocalState\\Assets');;
let destPath = path.join(os.homedir(), '\\Pictures\\Windows聚焦\\');
fileList(sourcePath);

/**
 * 递归函数，提取目录下的文件
 * @param string filePath 
 */
function fileList(filePath) {
    let number = 0;
    let files = fs.readdirSync(filePath);
    files.forEach((file, index) => {
        let fPath = path.join(filePath, file);
        let fileStatus = fs.statSync(fPath);
        if (fileStatus.isDirectory()) {
            fileList(fPath);
        } else {
            try {
                let savePath = path.join(destPath, file + '.jpg');
                if (!fs.existsSync(destPath)) {
                    fs.mkdirSync(destPath);
                }
                let dimensions = sizeOf(fPath);
                if ((dimensions.width == 1920 && dimensions.height == 1080) || (dimensions.height == 1920 && dimensions.width == 1080)) {
                    let read = fs.readFileSync(fPath);
                    fs.writeFileSync(savePath, read);
                    number ++;
                }
            } catch (e) {
                //console.warn(e.message.warn);
            }
        }
    });
    console.log(`总计提取了 ${number} 张图片。`.data);
}
